package main

import "fmt"

type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

// 前序遍历
func preorderTraversal(root *TreeNode) {
    if root == nil {
        return
    }
    fmt.Printf("%d ", root.Val)
    preorderTraversal(root.Left)
    preorderTraversal(root.Right)
}

// 中序遍历
func inorderTraversal(root *TreeNode) {
    if root == nil {
        return
    }
    inorderTraversal(root.Left)
    fmt.Printf("%d ", root.Val)
    inorderTraversal(root.Right)
}

// 后序遍历
func postorderTraversal(root *TreeNode) {
    if root == nil {
        return
    }
    postorderTraversal(root.Left)
    postorderTraversal(root.Right)
    fmt.Printf("%d ", root.Val)
}

func main() {
    // 创建二叉树
    node1 := &TreeNode{Val: 1}
    node2 := &TreeNode{Val: 2}
    node3 := &TreeNode{Val: 3}
    node4 := &TreeNode{Val: 4}
    node5 := &TreeNode{Val: 5}
    node6 := &TreeNode{Val: 6}
    node7 := &TreeNode{Val: 7}
    node8 := &TreeNode{Val: 8}
    node9 := &TreeNode{Val: 9}
    node10 := &TreeNode{Val: 10}
    node1.Left = node2
    node1.Right = node3
    node2.Left = node4
    node2.Right = node5
    node3.Left = node6
    node3.Right = node7
    node4.Left = node8
    node4.Right = node9
    node7.Right = node10

    fmt.Println("前序遍历：")
    preorderTraversal(node1)
    fmt.Println()

    fmt.Println("中序遍历：")
    inorderTraversal(node1)
    fmt.Println()

    fmt.Println("后序遍历：")
    postorderTraversal(node1)
    fmt.Println()
}